package com.itzhigui.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.itzhigui.entity.User;
import com.itzhigui.mapper.UserMapper;
import com.itzhigui.service.UserService;
import com.itzhigui.utils.PageUtil;
import com.itzhigui.utils.ResponseEnum;
import com.itzhigui.utils.ResponseUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public ResponseUtil findAll() {
        return ResponseUtil.get(ResponseEnum.USER_ALL_SUCCESS, userMapper.findAll());
    }

    @Override
    public ResponseUtil add(User user) {
        return userMapper.add(user).intValue() > 0 ?
                ResponseUtil.get(ResponseEnum.USER_ADD_SUCCESS) : ResponseUtil.get(ResponseEnum.USER_ADD_FAILURE);
    }

    @Override
    public ResponseUtil apiLogin(User user) {
        //登录用户
        User loginUser = userMapper.findByUsername(user.getUsername());
        ResponseUtil r = null;
        if (null == loginUser) {
            //用户名不存在
            r = ResponseUtil.get(ResponseEnum.USER_API_LOGIN_1);

        } else if (!loginUser.getPassword().equals(
                SaSecureUtil.md5BySalt(user.getPassword(), loginUser.getSalt()))
        ) {
            //提交的密码和数据库的密码不相同，登录失败
            r = ResponseUtil.get(ResponseEnum.USER_API_LOGIN_2);
        } else if (loginUser.getRole().intValue() != 1) {
            //用户名和密码是匹配的，判断角色不匹配
            r = ResponseUtil.get(ResponseEnum.USER_API_LOGIN_3);
        } else {
            //登录成功
            //密码设置成空
            loginUser.setPassword(null);
            loginUser.setSalt(null);
            r = ResponseUtil.get(ResponseEnum.USER_API_LOGIN_SUCCESS, loginUser);
        }
        return r;
    }

    @Override
    public ResponseUtil addApi(User user) {
        //=================没置user中相关参数,有些将来从页面提交=====================
        user.setEmail("110110@qq.com"); //设置邮箱
        user.setQuestion("喜欢哪个女明星？");  //设置找回密码的问题
        user.setAnswer("喜欢AB");
        user.setRole(1);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        String salt = UUID.randomUUID().toString().replaceAll("-", "");
        System.out.println(salt);
        user.setSalt(salt);
        //借助盐给密码加密
        String oldPassword = user.getPassword();
        user.setPassword(SaSecureUtil.md5BySalt(oldPassword, salt));
        //=================没置user中相关参数,有些将来从页面提交=====================
        return userMapper.add(user).intValue() > 0 ?
                ResponseUtil.get(ResponseEnum.USER_ADD_SUCCESS) : ResponseUtil.get(ResponseEnum.USER_ADD_FAILURE);
    }

    @Override
    public ResponseUtil fondAdminByName(User user) {
        User loginUser = userMapper.findByUsername(user.getUsername());
        ResponseUtil r = null;
        if (null == loginUser) {
            //用户名不存在
            r = ResponseUtil.get(ResponseEnum.USER_API_LOGIN_1);

        } else if (!loginUser.getPassword().equals(
                SaSecureUtil.md5BySalt(user.getPassword(), loginUser.getSalt()))
        ) {
            //提交的密码和数据库的密码不相同，登录失败
            r = ResponseUtil.get(ResponseEnum.USER_API_LOGIN_2);
        } else if (loginUser.getRole().intValue() != 0) {
            //用户名和密码是匹配的，判断角色不匹配
            r = ResponseUtil.get(ResponseEnum.USER_ADMIN_LOGOUT_ERROR_ROLE);
        } else {
            //登录成功
            //密码设置成空
            loginUser.setPassword(null);
            loginUser.setSalt(null);
            r = ResponseUtil.get(ResponseEnum.USER_ADMIN_LOGOUT_SUCCESS, loginUser);
        }
        return r;
    }

    @Override
    public ResponseUtil find(Integer page, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        Integer startIndexNo = (page - 1) * pageSize;
        map.put("startIndexNo",startIndexNo);
        map.put("pageSize",pageSize);
        PageUtil<User> pageUtil = new PageUtil<>();
        pageUtil.setCurrentPageNo(page);
        pageUtil.setPageSize(pageSize);
        Integer totalCount = userMapper.findCount(map);
        pageUtil.setTotalCount(totalCount);
        List<User> data = userMapper.find(map);
        pageUtil.setData(data);

        return ResponseUtil.get(ResponseEnum.USER_FIND_SUCCESS,pageUtil);
    }
}
